package com.gy.hsim.bservice.userCenter.restful;

import java.io.IOException;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gy.hsim.bservice.ecomInfo.bean.SaleAndOperatorRelation;
import com.gy.hsim.bservice.ecomInfo.bean.OperatorRelationAndUcInfo;
import com.gy.hsim.bservice.ecomInfo.service.EcomInfoService;
import com.gy.hsim.bservice.framework.bean.QueryVerifyReponse;
import com.gy.hsim.bservice.framework.enums.MessageResultEnum;
import com.gy.hsim.bservice.framework.mapper.FriendInfoMapper;
import com.gy.hsim.bservice.framework.mapper.SaleRelationMapper;
import com.gy.hsim.bservice.framework.restful.BaseController;
import com.gy.hsim.bservice.friendInfo.bean.Friend;
import com.gy.hsim.bservice.friendInfo.bean.FriendUcInfo;
import com.gy.hsim.bservice.friendInfo.bean.QueryFriend2UsercData;
import com.gy.hsim.bservice.friendInfo.bean.QueryFriendReq;
import com.gy.hsim.bservice.friendInfo.service.FriendInfoService;
import com.gy.hsim.bservice.message.bean.Message;
import com.gy.hsim.bservice.userCenter.bean.UserCenterParam;
import com.gy.hsxt.common.bean.Page;
import com.gy.hsxt.common.bean.PageData;
import com.gy.hsxt.common.exception.HsException;
import com.gy.hsxt.uc.as.api.common.IUCAsAuthService;
import com.gy.hsxt.uc.as.api.common.IUCAsNetworkInfoService;
import com.gy.hsxt.uc.as.api.operator.IUCAsOperatorService;
import com.gy.hsxt.uc.as.bean.common.AsNetworkInfo;
import com.gy.hsxt.uc.as.bean.enumerate.ChannelTypeEnum;
import com.gy.hsxt.uc.as.bean.operator.AsOperator;
import com.gy.hsxt.uc.as.bean.operator.AsQueryOperatorCondition;
import com.gy.hsxt.uc.search.api.IUCUserInfoSearchService;
import com.gy.hsxt.uc.search.api.IUCUserInfoService;
import com.gy.hsxt.uc.search.bean.Paginate;
import com.gy.hsxt.uc.search.bean.SearchResult;
import com.gy.hsxt.uc.search.bean.SearchUserInfo;
import com.gy.hsxt.uc.search.bean.SearchUserRole;
import com.gy.hsxt.uc.search.bean.SearchUserTypeEnum;

/**
 * 
 * @ClassName: UserCenterController
 * @Description: 用户中心Controller
 * @author quanrj
 * @date 2016-2-25 上午 11:59:19
 */
@Controller
@RequestMapping("/userCenter")
public class UserCenterController extends BaseController {

	private static final Logger log = LoggerFactory
			.getLogger(UserCenterController.class);
	@Autowired
	private IUCUserInfoSearchService userInfoSearchService;
	@Autowired
	private IUCAsAuthService authService;
	@Autowired
	private IUCAsOperatorService iUCAsOperatorService;
	@Autowired
	private IUCUserInfoService iUCUserInfoService;
	@Autowired
	private IUCAsNetworkInfoService iUCAsNetworkInfoService;	
	@Autowired
	private EcomInfoService ecomInfoService;
	@Autowired
	private FriendInfoService friendInfoService;

	/**
	 *消费者后端鉴权URL : http://localhost:9090/hsim-bservice/userCenter/consumerAuth
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/consumerAuth", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse consumerAuth(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		
		JSONObject object = (JSONObject)request.getAttribute("object");
		UserCenterParam userCenterParam = JSON.toJavaObject(object, UserCenterParam.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);

		/**渠道类型
		1：WEB（web端）
		2：POS(POS机)
		3：MCR(刷卡器)
		4：MOBILE(手机)
		5：HSPAD(互生平板)
		6：SYS(互生平台接入)
		7：IVR(呼叫中心)
		8：THIRDPART（第三方接入）*/
		ChannelTypeEnum channelType = null;
		/*WEB, POS, MCR, MOBILE, HSPAD, SYS, IVR, THIRDPART*/
		switch (userCenterParam.getChannelType()) {
		case 1:
			channelType = ChannelTypeEnum.WEB;
			break;
		case 2:
			channelType = ChannelTypeEnum.POS;
			break;
		case 3:
			channelType = ChannelTypeEnum.MCR;
			break;
		case 4:
			channelType = ChannelTypeEnum.MOBILE;
			break;
		case 5:
			channelType = ChannelTypeEnum.HSPAD;
			break;
		case 6:
			channelType = ChannelTypeEnum.SYS;
			break;
		case 7:
			channelType = ChannelTypeEnum.IVR;
			break;
		case 8:
			channelType = ChannelTypeEnum.THIRDPART;
			break;
		default:
			break;
		}
		/*String custId = "08186630000162206994400256";
		ChannelTypeEnum channelType = ChannelTypeEnum.MOBILE;
		String loginToken = "8d3d433e60aae831da2001b0f58e7d2cc7c221d26aa83b8e24ed054a144b6a38";*/		
		try{
			authService.consumerAuth(userCenterParam.getCustId(), channelType, userCenterParam.getLoginToken());
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage("鉴权成功");
		}
		catch(HsException e){
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage("鉴权失败");
		}
				
		return resp;

		/*
		 * JSONObject object = (JSONObject)request.getAttribute("object");
		 * 
		 * QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		 */

	}
	
	/**
	 *企业操作员后端鉴权URL : http://localhost:9090/hsim-bservice/userCenter/operatorAuth
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/operatorAuth", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse operatorAuth(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		UserCenterParam userCenterParam = JSON.toJavaObject(object, UserCenterParam.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);

		/**渠道类型
		1：WEB（web端）
		2：POS(POS机)
		3：MCR(刷卡器)
		4：MOBILE(手机)
		5：HSPAD(互生平板)
		6：SYS(互生平台接入)
		7：IVR(呼叫中心)
		8：THIRDPART（第三方接入）*/
		ChannelTypeEnum channelType = null;
		/*WEB, POS, MCR, MOBILE, HSPAD, SYS, IVR, THIRDPART*/
		switch (userCenterParam.getChannelType()) {
		case 1:
			channelType = ChannelTypeEnum.WEB;
			break;
		case 2:
			channelType = ChannelTypeEnum.POS;
			break;
		case 3:
			channelType = ChannelTypeEnum.MCR;
			break;
		case 4:
			channelType = ChannelTypeEnum.MOBILE;
			break;
		case 5:
			channelType = ChannelTypeEnum.HSPAD;
			break;
		case 6:
			channelType = ChannelTypeEnum.SYS;
			break;
		case 7:
			channelType = ChannelTypeEnum.IVR;
			break;
		case 8:
			channelType = ChannelTypeEnum.THIRDPART;
			break;
		default:
			break;
		}
		/*String custId = "08186630000162206994400256";
		ChannelTypeEnum channelType = ChannelTypeEnum.MOBILE;
		String loginToken = "8d3d433e60aae831da2001b0f58e7d2cc7c221d26aa83b8e24ed054a144b6a38";*/		
		try{
			authService.operatorAuth(userCenterParam.getCustId(), userCenterParam.getEntResNo(), channelType, userCenterParam.getLoginToken());
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage("鉴权成功");
		}
		catch(HsException e){
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage("鉴权失败");
		}
				
		return resp;

		/*
		 * JSONObject object = (JSONObject)request.getAttribute("object");
		 * 
		 * QueryFriendReq req = JSON.toJavaObject(object, QueryFriendReq.class);
		 */

	}
	
	/**
	 * 查询企业下的所有操作员请求URL: http://localhost:9090/hsim-bservice/userCenter/listOperByEntCustId
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/listOperByEntCustId", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse listOperByEntCustId(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		UserCenterParam userCenterParam = JSON.toJavaObject(object, UserCenterParam.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);

		try {
			SearchResult<SearchUserInfo> result = userInfoSearchService.getOpersByEntCustId(userCenterParam.getEntResNo());			
			//把营业点、营业点名称、角色编号返回
			List<OperatorRelationAndUcInfo> operatorRelationAndUcInfoList = new ArrayList<OperatorRelationAndUcInfo>();
			for(SearchUserInfo searchUserInfo : result.getList()){
				String custId = searchUserInfo.getCustId();
				String enterprise = custId.substring(0,11);
				String operator = custId.substring(11,15);
				List<SaleAndOperatorRelation> saleAndOperatorRelationList = new ArrayList<SaleAndOperatorRelation>();
				saleAndOperatorRelationList = ecomInfoService.queryOperatorInfo(enterprise, operator);
				
				//重新给用户角色设值,原营业点关联操作员表因为用户中心3.0原因不能把角色数据正确同步
				List<SearchUserRole> userRoleList = new ArrayList<SearchUserRole> ();
				String userRole = "";
				String userRoleName = "";
				userRoleList = searchUserInfo.getUserRole();
				if(userRoleList !=null && userRoleList.size()>0){
					for(int i=0;i<userRoleList.size();i++){
						userRole = userRole + userRoleList.get(i).getRoleId()+",";
						userRoleName = userRoleName + userRoleList.get(i).getRoleName()+",";
					}
				}
				if(userRole !=""){
					userRole = userRole.substring(0,userRole.length()-1);
				}
				if(userRoleName !=""){
					userRoleName = userRoleName.substring(0,userRoleName.length()-1);
				}
				
				//营业点关联操作员信息关联上操作员的custid,并且重新组装上用户角色和用户角色名
				for(SaleAndOperatorRelation saleAndOperatorRelation : saleAndOperatorRelationList){
					saleAndOperatorRelation.setCustId(custId);
					saleAndOperatorRelation.setRoleid(userRole);
					saleAndOperatorRelation.setRoleName(userRoleName);
				}
												
				OperatorRelationAndUcInfo operatorRelationAndUcInfo = new OperatorRelationAndUcInfo();
				operatorRelationAndUcInfo.setSearchUserInfo(searchUserInfo);
				operatorRelationAndUcInfo.setSaleAndOperatorRelationList(saleAndOperatorRelationList);
				operatorRelationAndUcInfoList.add(operatorRelationAndUcInfo);
			}
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());
			resp.setRows(operatorRelationAndUcInfoList);
		} catch (Exception e) {
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		return resp;
		
	}
	
	/**
	 * 查询企业下的所有操作员(分页)请求URL: http://localhost:9090/hsim-bservice/userCenter/listOperators
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/listOperators", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse listOperators(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		UserCenterParam userCenterParam = JSON.toJavaObject(object, UserCenterParam.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);
		
		AsQueryOperatorCondition asQueryOperatorCondition = new AsQueryOperatorCondition();
		asQueryOperatorCondition.setEntCustId(userCenterParam.getCustId());
		asQueryOperatorCondition.setRealName(userCenterParam.getRealName());
		asQueryOperatorCondition.setUserName(userCenterParam.getUserName());
		
		Page page = null;
		if(userCenterParam.getPageSize() != null && !userCenterParam.getPageSize().equals("")){
			page = new Page(userCenterParam.getPageSize());
			if(userCenterParam.getCurrentPage() != null && !userCenterParam.getCurrentPage().equals("")){
				page.setCurPage(userCenterParam.getCurrentPage());
			}			
		}
		
		try {
			PageData<AsOperator> pageData = iUCAsOperatorService.listOperators(asQueryOperatorCondition, page);
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());
			resp.setRows(pageData.getResult());
		} catch (Exception e) {
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		return resp;
		
	}
	
	/**
	 * 根据客户号搜索用户信息URL: http://localhost:9090/hsim-bservice/userCenter/queryUserInfoBycustId
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/queryUserInfoBycustId", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse queryUserInfoBycustId(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		UserCenterParam userCenterParam = JSON.toJavaObject(object, UserCenterParam.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);

		String custId = userCenterParam.getCustId();
		//String custId = "06011000000000020160109";
		List<OperatorRelationAndUcInfo> operatorRelationAndUcInfoList = new ArrayList<OperatorRelationAndUcInfo>();
		try {
			SearchUserInfo SearchUserInfo = userInfoSearchService.getById(custId);
			OperatorRelationAndUcInfo operatorRelationAndUcInfo = new OperatorRelationAndUcInfo();
			operatorRelationAndUcInfo.setSearchUserInfo(SearchUserInfo);
			if (SearchUserInfo != null && SearchUserInfo.getCustId() != null && !SearchUserInfo.getCustId().equals("")) {
				if(SearchUserInfo.getUserType() != 0 && SearchUserInfo.getUserType() != 1){    //持卡人和非持卡人不去查询营业点信息
					String custIdParam = SearchUserInfo.getCustId();
					String enterprise = custIdParam.substring(0,11);
					String operator = custIdParam.substring(11,15);
					List<SaleAndOperatorRelation> saleAndOperatorRelationList = new ArrayList<SaleAndOperatorRelation>();
					saleAndOperatorRelationList = ecomInfoService.queryOperatorInfo(enterprise, operator);
					//营业点关联操作员信息关联上操作员的custid
					for(SaleAndOperatorRelation saleAndOperatorRelation : saleAndOperatorRelationList){
						saleAndOperatorRelation.setCustId(custId);
					}
					operatorRelationAndUcInfo.setSaleAndOperatorRelationList(saleAndOperatorRelationList);
				}				
			}
			operatorRelationAndUcInfoList.add(operatorRelationAndUcInfo);
			
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());
			resp.setRows(operatorRelationAndUcInfoList);
		} catch (Exception e) {
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		return resp;
		
	}
	
	/**
	 * 根据条件搜索消费者信息URL: http://localhost:9090/hsim-bservice/userCenter/getCustomersByKeyword
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/getCustomersByKeyword", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse getCustomersByKeyword(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		UserCenterParam userCenterParam = JSON.toJavaObject(object, UserCenterParam.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);

		SearchUserInfo searchUserInfo = new SearchUserInfo();
		Paginate paginate;
		if(userCenterParam.getPageSize() != null && !userCenterParam.getPageSize().equals("")){
			paginate = new Paginate(userCenterParam.getPageSize());
			if(userCenterParam.getCurrentPage() != null && !userCenterParam.getCurrentPage().equals("")){
				paginate.setCurrentPage(userCenterParam.getCurrentPage());
			}			
			searchUserInfo.setPaginate(paginate);    //设置分页
		}
		
		//如果搜索关键字不超过2个字符，则不让搜索
		if(userCenterParam.getKeyword() == null || userCenterParam.getKeyword().equals("") || userCenterParam.getKeyword().length() < 2){
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage("搜索关键字不能少于2个字节");
		}
		
		searchUserInfo.setKeyword(userCenterParam.getKeyword());
		searchUserInfo.setAgeScope(userCenterParam.getAgeScope());
		searchUserInfo.setProvince(userCenterParam.getProvince());
		searchUserInfo.setCity(userCenterParam.getCity());
				
		try {
			int totalRows = 0;
			List<SearchUserInfo> SearchUserInfolist = new ArrayList<SearchUserInfo>();
			SearchResult<SearchUserInfo> searchResult = userInfoSearchService.getCustomersByKeyword(searchUserInfo);
			if(searchResult != null){
				totalRows = Integer.parseInt(String.valueOf(searchResult.getPaginate().getTotalNum()));
				SearchUserInfolist = searchResult.getList();
				//搜索的时候把客户自己账号信息屏蔽
				for(SearchUserInfo SearchUserInfo : SearchUserInfolist){
					if(SearchUserInfo.getCustId().equals(userCenterParam.getCustId())){
						SearchUserInfolist.remove(SearchUserInfo);
						break;
					}
				}
			}
			
			//给用户添加互动的好友状态用于前端标记
			List<FriendUcInfo> friendUcInfoList = new ArrayList<FriendUcInfo>();
			if(SearchUserInfolist !=null && SearchUserInfolist.size()>0){
				List<Friend> friendsList = new ArrayList<Friend>();
				friendsList = friendInfoService.queryUcRelFriend(userCenterParam.getCustId());
				for(SearchUserInfo searchUserInfoSub : SearchUserInfolist){
					FriendUcInfo friendUcInfo = new FriendUcInfo();
					friendUcInfo.setSearchUserInfo(searchUserInfoSub);
					String accountId = searchUserInfoSub.getResNo();
					for(Friend friend : friendsList){
						if(friend.getFriendId().indexOf(accountId)>0){
							friendUcInfo.setFriendStatus(String.valueOf(friend.getFriendStatus()));
							break;
						}
					}
					friendUcInfoList.add(friendUcInfo);
				}
			}
			
			//遍历friendUcInfoList，把好友状态为空的设置成"0"，方便排序
			for(FriendUcInfo friendUcInfoSetting : friendUcInfoList){
				if(friendUcInfoSetting.getFriendStatus() == null || friendUcInfoSetting.getFriendStatus().equals("")){
					friendUcInfoSetting.setFriendStatus("0");
				}
			}
			
			//对列表进行排序
			Collections.sort(friendUcInfoList, new Comparator<FriendUcInfo>() {
	            public int compare(FriendUcInfo arg0, FriendUcInfo arg1) {
	                return String.valueOf(arg0.getFriendStatus()).compareTo(String.valueOf(arg1.getFriendStatus()));
	            }
	        });	
									
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());
			resp.setRows(friendUcInfoList);
			resp.setTotalRows(totalRows);
		} catch (Exception e) {
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		return resp;
		
	}
	
	/**
	 * 搜索平台下属的企业和用户URL: http://localhost:9090/hsim-bservice/userCenter/getCustIdsByPlatform
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/getCustIdsByPlatform", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse getCustIdsByPlatform(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		UserCenterParam userCenterParam = JSON.toJavaObject(object, UserCenterParam.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);

		SearchUserInfo searchUserInfo = new SearchUserInfo();
		Paginate paginate = null;
		if(userCenterParam.getPageSize() != null && !userCenterParam.getPageSize().equals("")){
			paginate = new Paginate(userCenterParam.getPageSize());
			if(userCenterParam.getCurrentPage() != null && !userCenterParam.getCurrentPage().equals("")){
				paginate.setCurrentPage(userCenterParam.getCurrentPage());
			}			
			searchUserInfo.setPaginate(paginate);    //设置分页
		}
		/*int searchType = -2;//-1 全部;5 管理公司;4 服务公司;3 托管企业;2 成员企业 
		Paginate paginate = new Paginate(1000);*/
		int searchType = userCenterParam.getSearchType();
		/**		 
		 *  SearchUserTypeEnum枚举值：
		 *  NO_CARDER(0)    非持卡人 
		 *  CARDER(1)       持卡人 
		 *  ENT_MEMEBER(2)  成员企业 
		 *  ENT_TRUSTTEE(3) 托管企业 
		 *  ENT_SVR(4)      服务公司 
		 *  ENT_MGT(5)      管理公司 
		 *  ALL(-1)         所有类型的用户，包括消费者；成员企业、托管企业、服务公司、管理公司的超级管理员 
		 *  CONSUMER(-2)    消费者，包括持卡人和非持卡人 
		 *  ENT(-3)         成员企业和托管企业 
		 */				
		try {
			int totalRows = 0;
			SearchResult<String> result = userInfoSearchService.getCustIdsByPlatform(searchType, paginate);
			totalRows = Integer.parseInt(String.valueOf(result.getPaginate().getTotalNum()));
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());
			List<String> resultList = result.getList();
			List<String> resultFillterList = new ArrayList<String>();
			if(resultList != null && resultList.size() >0){
				//把不是19位的数据清除				
				for(String resultStr : resultList){
					if(resultStr.length() == 19){
						resultFillterList.add(resultStr);
					}
				}
			}			
			resp.setRows(resultFillterList);
			resp.setTotalRows(totalRows);
		} catch (Exception e) {
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		return resp;
		
	}
	
	/**
	 * 根据管理公司资源号查询其下所有的服务公司管理员的操作号URL: http://localhost:9090/userCenter/hsim-bservice/getCustIdsByMgtEntResNo
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/getCustIdsByMgtEntResNo", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse getCustIdsByMgtEntResNo(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		UserCenterParam userCenterParam = JSON.toJavaObject(object, UserCenterParam.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);

		SearchUserInfo searchUserInfo = new SearchUserInfo();
		Paginate paginate = null;
		if(userCenterParam.getPageSize() != null && !userCenterParam.getPageSize().equals("")){
			paginate = new Paginate(userCenterParam.getPageSize());
			if(userCenterParam.getCurrentPage() != null && !userCenterParam.getCurrentPage().equals("")){
				paginate.setCurrentPage(userCenterParam.getCurrentPage());
			}			
			searchUserInfo.setPaginate(paginate);    //设置分页
		}
		/*String entResNo = "06000000000";
		Paginate paginate = new Paginate(1000);*/
		String entResNo = userCenterParam.getEntResNo();			
		try {
			SearchResult<String> result = userInfoSearchService.getCustIdsByMgtEntResNo(entResNo, paginate);
			int totalRows = 0;
			totalRows = Integer.parseInt(String.valueOf(result.getPaginate().getTotalNum()));
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());
			resp.setRows(result.getList());
			resp.setTotalRows(totalRows);
		} catch (Exception e) {
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		return resp;
		
	}
	
	/**
	 * 根据服务公司资源号查询 URL: http://localhost:9090/hsim-bservice/userCenter/getCustIdsByServiceEntResNo
	 *  根据服务公司资源号查询：3 托管企业
	 *  根据服务公司资源号查询：4 成员企业
	 *  根据服务公司资源号查询：-3 托管企业+成员企业
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/getCustIdsByServiceEntResNo", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse getCustIdsByServiceEntResNo(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		UserCenterParam userCenterParam = JSON.toJavaObject(object, UserCenterParam.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);

		SearchUserInfo searchUserInfo = new SearchUserInfo();
		Paginate paginate = null;
		if(userCenterParam.getPageSize() != null && !userCenterParam.getPageSize().equals("")){
			paginate = new Paginate(userCenterParam.getPageSize());
			if(userCenterParam.getCurrentPage() != null && !userCenterParam.getCurrentPage().equals("")){
				paginate.setCurrentPage(userCenterParam.getCurrentPage());
			}			
			searchUserInfo.setPaginate(paginate);    //设置分页
		}
		/*String entResNo = "06002000000";
		int searchType = SearchUserTypeEnum.ENT.getType();//3 托管企业; 2 成员企业; -3 托管企业+成员企业*/
		String entResNo = userCenterParam.getEntResNo();
		int searchType = userCenterParam.getSearchType();
		/**		 
		 *  SearchUserTypeEnum枚举值：
		 *  NO_CARDER(0)    非持卡人 
		 *  CARDER(1)       持卡人 
		 *  ENT_MEMEBER(2)  成员企业 
		 *  ENT_TRUSTTEE(3) 托管企业 
		 *  ENT_SVR(4)      服务公司 
		 *  ENT_MGT(5)      管理公司 
		 *  ALL(-1)         所有类型的用户，包括消费者；成员企业、托管企业、服务公司、管理公司的超级管理员 
		 *  CONSUMER(-2)    消费者，包括持卡人和非持卡人 
		 *  ENT(-3)         成员企业和托管企业 
		 */
		try {
			SearchResult<String> result = userInfoSearchService.getCustIdsByServiceEntResNo(entResNo, searchType, paginate);
			int totalRows = 0;
			totalRows = Integer.parseInt(String.valueOf(result.getPaginate().getTotalNum()));
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());
			resp.setRows(result.getList());
			resp.setTotalRows(totalRows);
		} catch (Exception e) {
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		return resp;
		
	}
	
	/**
	 * 修改用户中心用户信息URL: http://localhost:9090/userCenter/hsim-bservice/updateUserInfo
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse updateUserInfo(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		SearchUserInfo searchUserInfo = JSON.toJavaObject(object, SearchUserInfo.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);
		
		try {
			iUCUserInfoService.update(searchUserInfo);			
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		return resp;
		
	}
	
	/**
	 * 修改用户中心网络信息URL: http://localhost:9090/userCenter/hsim-bservice/updateNetworkInfo
	 * 
	 * @param request
	 * @param response
	 * @return 响应返回实体类，含返回码、响应消息
	 */
	@RequestMapping(value = "/updateNetworkInfo", method = RequestMethod.POST)
	public @ResponseBody
	QueryVerifyReponse updateNetworkInfo(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		QueryVerifyReponse resp = new QueryVerifyReponse();
		/*InputStream is = null;
		is = request.getInputStream();
		byte bytes[] = new byte[request.getContentLength()];
		is.read(bytes);
		// 得到请求中的内容区数据（以CharacterEncoding解码）
		String jsonStr = new String(bytes, "utf-8");
		System.out.println("【接收到的json】：" + jsonStr);
		log.debug("【接收到的json】：" + jsonStr);

		UserCenterParam userCenterParam = JSON.toJavaObject(
				JSON.parseObject(jsonStr), UserCenterParam.class);*/
		JSONObject object = (JSONObject)request.getAttribute("object");
		AsNetworkInfo netWorkInfo = JSON.toJavaObject(object, AsNetworkInfo.class);
		System.out.println("【接收到的json】：" + object);
		log.debug("【接收到的json】：" + object);
		
		try {
			iUCAsNetworkInfoService.updateNetworkInfo(netWorkInfo);
			resp.setRetCode(MessageResultEnum.EXEC_SUCCESS.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_SUCCESS.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			resp.setRetCode(MessageResultEnum.EXEC_FAILD.getRetCode());
			resp.setMessage(MessageResultEnum.EXEC_FAILD.getMessage());
		}

		return resp;
		
	}
}
